public class Selflearn_20 {
    public static void main(String[] args){
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append("aiuefgiuwe");
        System.out.println(stringBuilder);
        String s= "aaaaaa";
        stringBuilder.append(s);
        System.out.println(stringBuilder);
        Delete(stringBuilder,"a");
        System.out.println(stringBuilder);
    }
    public static void delete(StringBuilder stringBuilder,String s){
        for(int i=0;i<stringBuilder.length()-s.length()+1;i++){
            try {
                do {
                    String a = stringBuilder.substring(i, i + s.length());
                    if (a.equals(s)) {
                        stringBuilder.delete(i, i + s.length());
                    }
                }while (stringBuilder.substring(i, i + s.length()).equals(s));
            }catch (IndexOutOfBoundsException e){
                return;
            }

        }
    }
    //下面是优化版的
    public static void Delete(StringBuilder stringBuilder,String s){
        for (int i=0;i<stringBuilder.length()-s.length()+1;){
            String a = stringBuilder.substring(i, i + s.length());
            if (a.equals(s)) {
                stringBuilder.delete(i, i + s.length());
            }else {
                i++;
            }
        }
    }
}
